package com.ly.web.controller.erp;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.heart.domain.MaterialEntity;
import com.ly.heart.domain.dto.CustomerAllocDto;
import com.ly.heart.domain.dto.CustomerDto;
import com.ly.heart.domain.dto.CustomerQueryDto;
import com.ly.heart.domain.vo.CustomerVo;
import com.ly.heart.service.king.base.CustomerPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.CustomerEntity;
import com.ly.heart.service.ICustomerService;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.common.core.page.TableDataInfo;

/**
 * 客户Controller
 *
 * @author cxl
 * @date 2024-05-14
 */
@Api ( value = "Customer", tags = "客户" )
@RestController
@RequestMapping ( "/heart/Customer" )
public class CustomerController extends BaseController {
    @Resource
    private ICustomerService customerEntityService;
    @Resource
    private CustomerPushService customerPushService;

    /**
     * 查询客户列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Customer:list')" )
    @GetMapping ( "/list" )
    @ApiOperation ( "查询客户列表" )
    public TableDataInfo list ( CustomerQueryDto customerQueryDto ) {
//        startPage();
        IPage<CustomerVo> customerVoIPage = customerEntityService.selectCustomerEntityList ( customerQueryDto );
        TableDataInfo tableDataInfo = new TableDataInfo ();
        tableDataInfo.setRows ( customerVoIPage.getRecords () );
        tableDataInfo.setTotal ( customerVoIPage.getTotal () );
        return tableDataInfo;
    }

    /**
     * 导出客户列表
     */
//    @PreAuthorize("@ss.hasPermi('heart:Customer:export')")
//    @Log(title = "客户", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation("导出客户列表")
//    public void export(HttpServletResponse response, CustomerEntity customerEntity) {
//        List<CustomerVo> list = customerEntityService.selectCustomerEntityList(customerEntity);
//        ExcelUtil<CustomerVo> util = new ExcelUtil<CustomerVo>(CustomerVo.class);
//        util.exportExcel(list, "客户数据");
//    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Customer:query')" )
    @GetMapping ( value = "/{id}" )
    @ApiOperation ( "获取客户详细信息" )
    public AjaxResult getInfo ( @PathVariable ( "id" ) String id ) {
        return AjaxResult.success ( customerEntityService.selectCustomerEntityById ( id ) );
    }

    /**
     * 客户基础表erp_customer_info，需上传附件可支撑多个附件
     * 新增客户时需要同时新增erp_customer_user_info用户、客户关联关系数据
     * 客户更多联系人数据erp_contacts_info多个
     */
    //@PreAuthorize ( "@ss.hasPermi('heart:Customer:add')" )
    @Log ( title = "暂存客户", businessType = BusinessType.INSERT )
    @PostMapping ( "draft" )
    @ApiOperation ( "暂存客户" )
    public AjaxResult draft ( @RequestBody CustomerDto customerDto ) {
        AjaxResult ajaxResult = customerEntityService.draft ( customerDto );
        return AjaxResult.success ( ajaxResult );
    }

    //@PreAuthorize ( "@ss.hasPermi('heart:Customer:add')" )
    @Log ( title = "提交客户", businessType = BusinessType.INSERT )
    @PostMapping ( "submit" )
    @ApiOperation ( "提交客户" )
    public AjaxResult submit ( @RequestBody CustomerEntity customerEntity ) {
        AjaxResult ajaxResult = customerEntityService.submit ( customerEntity );
        return AjaxResult.success ( ajaxResult );
    }

    /**
     * 修改客户
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Customer:edit')" )
    @Log ( title = "客户", businessType = BusinessType.UPDATE )
    @PutMapping
    @ApiOperation ( "修改客户" )
    public AjaxResult edit ( @RequestBody CustomerDto customerDto ) {
        return customerEntityService.updateCustomerEntity ( customerDto );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:Customer:edit')" )
    @Log ( title = "客户", businessType = BusinessType.UPDATE )
    @PostMapping ( "updateSingle" )
    @ApiOperation ( "修改客户基本信息" )
    public AjaxResult updateSingle ( @RequestBody CustomerEntity customerEntity ) {
        return customerEntityService.updateSingle ( customerEntity );
    }

    /**
     * 删除客户
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Customer:remove')" )
    @Log ( title = "客户", businessType = BusinessType.DELETE )
    @DeleteMapping ( "/{ids}" )
    @ApiOperation ( "删除客户" )
    public AjaxResult remove ( @PathVariable String[] ids ) {
        return toAjax ( customerEntityService.deleteCustomerEntityByIds ( ids ) );
    }

    @ApiOperation ( "根据手机号/税号查重客户" )
    @GetMapping ( "/checkCustomer" )
    public AjaxResult checkCustomer ( CustomerEntity customerEntity ) {
        List<CustomerEntity> list = customerEntityService.list ( new LambdaQueryWrapper<CustomerEntity> ()
                .eq ( CustomerEntity :: getTelPhone, customerEntity.getTelPhone () )
                .eq ( CustomerEntity :: getTaxId, customerEntity.getTaxId () )
        );
        if ( ! CollectionUtils.isEmpty ( list ) ) {
            return AjaxResult.error ( "客户已存在是否继续录入" );
        } else {
            return AjaxResult.success ();
        }

    }

    @Log ( title = "禁用", businessType = BusinessType.UPDATE )
    @GetMapping ( "/disabled" )
    @ApiOperation ( "禁用/解禁物料" )
    public AjaxResult disabled ( String id ) {
        return customerEntityService.disabled ( id );
    }


    @ApiOperation ( "选择调用的客户列表" )
    @GetMapping ( "/menuCustomerList" )
    public AjaxResult customerList ( CustomerEntity customerEntity ) {
        Page<CustomerEntity> page = new Page<> ( customerEntity.getPage (), customerEntity.getLimit () );
        Page<CustomerEntity> entityPage = customerEntityService.page ( page, new LambdaQueryWrapper<CustomerEntity> ()
                .eq ( CustomerEntity :: getType, ConstUtil.CUSTOMER_SIGNED )
                .eq ( CustomerEntity :: getIsUsed, ConstUtil.IS_USED_YES )
                .eq ( CustomerEntity :: getIsDisabled, 1 )
                .like ( StringUtils.isNotBlank ( customerEntity.getCustomerName () ), CustomerEntity :: getCustomerName, customerEntity.getCustomerName () )
                .like ( StringUtils.isNotBlank ( customerEntity.getCustomerCode () ), CustomerEntity :: getCustomerCode, customerEntity.getCustomerCode () )
        );
        return AjaxResult.success ( entityPage );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:Customer:remove')" )
    @Log ( title = "分盘客户给指定业务员" )
    @PostMapping ( "alloc" )
    @ApiOperation ( "分盘客户给指定业务员" )
    public AjaxResult alloc ( @RequestBody CustomerAllocDto customerAllocDto ) {
        return customerEntityService.alloc ( customerAllocDto );
    }

    @GetMapping ( "push" )
    public AjaxResult pushCustomer ( String id ) {
        customerPushService.push ( id );
        return AjaxResult.success ( "推送客户成功" );
    }

    @GetMapping ( "pull" )
    public AjaxResult downloadCustomer () {
        customerPushService.pull ();
        return AjaxResult.success ( "拉取客户成功" );
    }

}
